package cn.wolfcode.luowowo.article.service.impl;
import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.DestinationTheme;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.domain.UserDest;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.mapper.DestinationThemeMapper;
import cn.wolfcode.luowowo.article.mapper.RegionMapper;
import cn.wolfcode.luowowo.article.mapper.UserDestMapper;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.common.query.DestinationThemeQueryObject;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.query.TravelQueryObject;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Service
public class DestinationServiceImpl implements IDestinationService {
    @Autowired
    private DestinationMapper destinationMapper;
    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private DestinationThemeMapper themeMapper;
    @Autowired
    private UserDestMapper userDestMapper;

    public PageInfo<Destination> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Destination> list = destinationMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    public List<Destination> listToasts(Long id) {
        List<Destination> list = new ArrayList<>();
        //1.查询该id对应的目的地
        Destination dest = destinationMapper.selectByPrimaryKey(id);
        list.add(dest);

        //2.取出该目的地的父级
        while (dest.getParent() != null) {
            dest = destinationMapper.selectByPrimaryKey(dest.getParent().getId());
            list.add(dest);
        }

        //3.反转
        Collections.reverse(list);
        return list;
    }

    public List<Destination> list() {
        return destinationMapper.selectAll();
    }

    public List<Destination> listByReginId(Long regionId) {
        Region region = regionMapper.selectByPrimaryKey(regionId);
        return destinationMapper.selectByIds(region.getRefIds());
    }

    public List<Destination> listHotDestByRegionId(Long regionId) {
        //区域的id大于0,查询国家
        if (regionId > 0) {
            return destinationMapper.selectHotByRegionId(regionId);
        }
        //区域id=-1的,国内的省份
        return destinationMapper.selectProvinces();
    }

    public Destination getCountry(Long destId) {
        List<Destination> toasts = listToasts(destId);
        return toasts.get(0);
    }

    public Destination getProvince(Long destId) {
        List<Destination> toasts = listToasts(destId);
        return toasts.size() >1 ? toasts.get(1) : null;
    }



    @Override
    public PageInfo<Destination> listInThemeIds(DestinationThemeQueryObject qo) {
        DestinationTheme month = themeMapper.selectByPrimaryKey(qo.getMonthId());
        DestinationTheme theme = themeMapper.selectByPrimaryKey(qo.getThemeId());
        DestinationTheme day = themeMapper.selectByPrimaryKey(qo.getDayId());

        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());

        List<Destination> dest = destinationMapper.selectInThemeIds
                (month==null?null:month.getRefIds(),
                 theme==null?null:theme.getRefIds(),
                 day==null?null:day.getRefIds());

        return new PageInfo<>(dest);
    }
    @Override
    public boolean isFavor(UserInfo user, Long travelId) {

        List<UserDest> userDests = userDestMapper.selectByUserIdAndDestId(user.getId(), travelId);

        if(userDests.size() == 0){
            UserDest ut = new UserDest();
            ut.setUserId(user.getId());
            ut.setDestId(travelId);
            userDestMapper.insert(ut);
            return true;
        }else{
            userDestMapper.deleteByUserIdAndDestId(user.getId(), travelId);
            return false;
        }
    }

    @Override
    public PageInfo<Destination> listByUserFavor(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Destination> list = destinationMapper.listByUserFavor(qo);
        return new PageInfo<>(list);
    }

    @Override
    public List<Destination> listIsHot() {
        return  destinationMapper.selectIsHot();
    }

    @Override
    public void deleteIsFavor(UserInfo user, Long travelId) {
        Long id = user.getId();
        userDestMapper.deleteIsFavor(id,travelId);
    }

    @Override
    public Destination get(Long destId) {
        return destinationMapper.selectByPrimaryKey(destId);
    }


    public List<Destination> listByThemeId(Long themeId) {
        DestinationTheme theme = themeMapper.selectByPrimaryKey(themeId);
        Long[] refIds = theme.getRefIds();
        return destinationMapper.selectByIds(refIds);
    }

    @Override
    public List<Destination> listByGuangDongR10() {
        return destinationMapper.selectByGuangDongR10();
    }
}